package demo.transaction;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import demo.Configuration;
import demo.model.Transaction;

@RestController
@RequestMapping("/txn")
public class TransactionServiceController {
	
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private Configuration configuration;
	
	
    @RequestMapping(value="/list/{name}")
    public ResponseEntity<List> getTransactionByAccountName(@PathVariable String name) {
    	
    	List<Transaction> txn = new ArrayList<Transaction>();
    	
    	txn.add(new Transaction("1",123456D, configuration.getRegion()));
    	
    	return ResponseEntity.ok(txn);
    }
    
    @RequestMapping(value="/list")
    public ResponseEntity<List>  getTransactionByAccountName(@RequestParam String name,@RequestParam String region) throws Exception{
    	
    	logger.info("account service is called.....region:{}",region);
    	
    	//simulate exception
    	if("UK".equals(region)){
    		throw new Exception("Unsupported region :" + region);
    	}
    	else if("US".equals(region)){
    		try{
    			Thread.sleep(5000L);
    		}catch(InterruptedException e){
    			logger.info("Wake up from sleep: " + region);
    		}
    	}
    	else if("UA".equals(region)){
    		try{
    			Thread.sleep(1000L);
    		}catch(InterruptedException e){
    			logger.info("Wake up from sleep: " + region);
    		}
    	}
    	
    	return getTransactionByAccountName(name);
    }
}

